/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        return construct(pre, vin, 0, pre.size()-1, 0, vin.size() - 1);
    }
    TreeNode* construct(vector<int>& pre, vector<int>& in, int prel, int prer,int inl, int inr) {
        if(inl > inr) return nullptr;
        int i = 0;
        for(; i < in.size(); ++i) {
            if(in[i] == pre[prel]) break;
        }
        int l_num = i - inl;
        TreeNode* node = new TreeNode(pre[prel]);
        node->left = construct(pre, in, prel + 1, prel + l_num, inl, i - 1);
        node->right = construct(pre, in, prel + l_num + 1, prer, i + 1, inr);
        return node; 
    }
};